#include "FlashComponent.h"
#include "VisualComponent.h"
#include "GameObject.h"

FlashComponent::FlashComponent(float p_fadeStrength) : m_fadeStrength(p_fadeStrength), m_blinkState(0), m_active(false){}
FlashComponent::~FlashComponent(void){}

void FlashComponent::init()
{
	m_visual = static_cast<VisualComponent*>(getOwner()->getComponent("VisualComponent"));
}

void FlashComponent::update(const GameTime& p_gameTime)
{
	if (m_active)
	{
		float alpha = static_cast<float>(m_visual->getSprite()->getColor().a);
		if (m_blinkState == 0)
		{
			alpha = std::max(alpha - (m_fadeStrength * p_gameTime.deltaTime()), 0.0f);
			if (alpha == 0.0f)
				m_blinkState = 1;
		}
		else if (m_blinkState == 1)
		{
			alpha = std::min(alpha + (m_fadeStrength * p_gameTime.deltaTime()), 255.0f);
			if (alpha == 255.0f)
				m_blinkState = 0;
		}
		m_visual->getSprite()->setColor(vn::Color(m_visual->getSprite()->getColor().r, m_visual->getSprite()->getColor().g, m_visual->getSprite()->getColor().b, static_cast<unsigned char>(alpha)));
	}
}

void FlashComponent::setActive(bool p_value)
{
	m_active = p_value;
	if (!p_value)
	{
		m_visual->getSprite()->setColor(vn::Color(m_visual->getSprite()->getColor().r, m_visual->getSprite()->getColor().g, m_visual->getSprite()->getColor().b, 255));
	}
}